home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / FinderFlocks / CParticle.cp < prev    next >
Encoding:
Text File  |  1996-06-22  |  1.8 KB  |  50 lines  |  [TEXT/CWIE]

  1. #include "CParticle.h"
  2.  
  3. /*  This class implements a simple ballistic particle in 2D with position, velocity,
  4.     and acceleration (all of which are "Fixed point Points"). The velocity and 
  5.     acceleration are treated as 2D vectors, and their units are pixels per frame and
  6.     pixels per frame per frame, respectively. This conveniently eliminates mass 
  7.     considerations in the math. The reason I do this is 'cuz it's faster. There are only
  8.     three routines: InitParticle, which sets the values of the variables; NextStep, which
  9.     moves the particle a time step of 1, calculating the new position and velocity; and 
  10.     Step, which takes an integer and moves that many steps. Note that there are no
  11.     drawing routines, and that all three variables are available only to subclasses. */
  12.     
  13. CParticle*    CParticle :: InitParticle(FloatPoint *ipos, FloatPoint *ivel, FloatPoint *iaccel)
  14. {
  15.     fPosition = *ipos;
  16.     fVelocity = *ivel;
  17.     fAcceleration = *iaccel;
  18.     return this;
  19. }
  20.  
  21. void CParticle :: NextStep()
  22. {
  23. /*    The equation for the new position is:  P(T) =  Po + VoT + 1/2 AT^^2. Here we assume a 
  24.     time step of 1, so it reduces to:  P = Po + Vo + A/2, all shifts and adds, which 
  25.     are fast and "Fixed-friendly" */
  26.     
  27.     double    h, v;        // horizontal and vertical positions
  28.     
  29.     h = this->fPosition.h;
  30.     v = this->fPosition.v;
  31.     h = h + this->fVelocity.h + (this->fAcceleration.h / 2); // velocity + half the acceleration
  32.     v = v + this->fVelocity.v + (this->fAcceleration.v / 2);
  33.     
  34. /*     The equation for the new velocity is: V(T) = Vo + AT, and T = 1 so... */
  35.     this->fVelocity.h += this->fAcceleration.h;
  36.     this->fVelocity.v += this->fAcceleration.v;
  37.     
  38.     // Set new position
  39.     fPosition.h = h;
  40.     fPosition.v = v;
  41. }
  42.  
  43. void CParticle :: Step(short numsteps)
  44. {
  45.     // Just single step the given number of times
  46.     while(numsteps-- > 0)
  47.         NextStep();
  48. }
  49.  
  50.